/**
 * Copyright (c)2014 快诺迅汇(长沙)网络技术股份有限公司。
 * 版权所有
 */
package com.lehe.controller;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.lehe.common.Contants;
import com.lehe.common.exceptions.LHServiceException;
import com.lehe.entry.UserSessionDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;

/**
 * 类的功能描述<br>
 *
 * @author 王丹
 * @FileName Basecontroller.java<br>
 * @Language Java<br>
 * @date 2016-07-22<br>
 */
public class Basecontroller {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected HttpSession session;

    protected UserSessionDto getUserSession(){
        Object o = session.getAttribute(Contants.SESSION_USER_KEY);
        if(null == o){
            return null;
        }else{
            return (UserSessionDto)o;
        }
    }

    protected Long getUserId(){
        Long userId = 0L;
        UserSessionDto sessionDto = getUserSession();
        if(null != sessionDto){
            userId = sessionDto.getUserId();
        }
        return userId;
    }
    
    protected Long getAgentId(){
        Long agentId = -1L;
        UserSessionDto sessionDto = getUserSession();
        if(null != sessionDto){
        	agentId = sessionDto.getAgentId();
        }
        return agentId;
    }

    protected String getWxOpenId(){
        String wxOpenId = "";
        UserSessionDto sessionDto = getUserSession();
        if(null != sessionDto){
            wxOpenId = sessionDto.getWxOpenId();
        }
        return wxOpenId;
    }
    
    protected UserSessionDto getUser(){
        UserSessionDto sessionDto = getUserSession();
        return sessionDto;
    }

    /**
     * 获取请求的内容
     * @param request 一个http请求对象
     * @return 获取的请求内容的字符串
     * @throws UnsupportedEncodingException
     * @throws IOException
     */
    protected String getRequestContent(HttpServletRequest request) throws IOException {
        BufferedReader rd = new BufferedReader(new InputStreamReader(request.getInputStream(), "utf-8"));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        rd.close();
        return sb.toString();
    }

    /**
     * 将内容从响应输出流中发送给请求方。
     * @param resp 响应对象
     * @param contentType 设置格式
     * @param string 响应的内容
     * @throws IOException
     */
    protected void makeResponse(HttpServletResponse resp, String contentType, String string) throws IOException {
        resp.setContentType(contentType);
        PrintWriter out = new PrintWriter(new OutputStreamWriter(resp.getOutputStream(), "utf-8"));
        out.write(string);
        out.flush();
        out.close();
    }

    protected String obj2Json(Object message) throws LHServiceException {
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            return mapper.writeValueAsString(message);
        } catch (IOException e) {
            e.printStackTrace();
            throw new LHServiceException("转换报文异常");
        }
    }

    protected <T> T json2Obj(String message, Class<T> clazz) throws LHServiceException{
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            return mapper.readValue(message, clazz);
        } catch (IOException e) {
            e.printStackTrace();
            throw new LHServiceException("转换报文异常");
        }
    }
}
